package com.atguigu.guli.service.oss.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.oss.service.FileService;
import com.atguigu.guli.service.oss.util.OssProperties;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.UUID;

/**
 * @author dyqstart
 * @create 2021-07-06-12:47
 * @since 1.0.0
 */
@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private OssProperties ossProperties;

    @Override
    public String upload(InputStream inputStream, String module, String oFilename) {
        String endpoint = ossProperties.getEndPoint();
        String keyid = ossProperties.getKeyId();
        String keysecret = ossProperties.getKeySecret();
        String bucketname = ossProperties.getBucketName();

        /*
        判断oss实例是否存在，如果不存在则创建，如果存在则获取
         */
        OSS ossClient = new OSSClientBuilder().build(endpoint,keyid,keysecret);
        if (!ossClient.doesBucketExist(bucketname)){
            //创建bucket
            ossClient.createBucket(bucketname);
            //设置oss实例的访问权限，公共读
            ossClient.setBucketAcl(bucketname, CannedAccessControlList.PublicRead);
        }

        //构建日期路径：avatar/2021/07/06/文件名
        String folder = new DateTime().toString("yyyy/MM/dd");

        //文件名：uuid.扩展名
        String fileName = UUID.randomUUID().toString();
        String fileExtension = oFilename.substring(oFilename.lastIndexOf("."));
        String key = module + "/" + folder + "/" + fileName + fileExtension;

        //文件上传到阿里云
        ossClient.putObject(ossProperties.getBucketName(),key,inputStream);

        //关闭OSSClient
        ossClient.shutdown();

        //返回url地址
        return "https://" + bucketname + "." + endpoint + "/" + key;

    }

    @Override
    public void removeFile(String url) {

        String endpoint = ossProperties.getEndPoint();
        String keyid = ossProperties.getKeyId();
        String keysecret = ossProperties.getKeySecret();
        String bucketname = ossProperties.getBucketName();

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint,keyid,keysecret);

        String host = "https://" + bucketname +"." + endpoint +"/";
        String objectName = url.substring(host.length());
        // 删除文件
        ossClient.deleteObject(bucketname,objectName);

        // 关闭OSSClient
        ossClient.shutdown();
    }

    @Override
    public String upload(String url, String module) {

        String endpoint = ossProperties.getEndPoint();
        String accessKeyId = ossProperties.getKeyId();
        String accessKeySecret = ossProperties.getKeySecret();
        String bucketName = ossProperties.getBucketName();

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        //目录的处理：分类 + 日期
        String folder = module + new DateTime().toString("/yyyy/MM/dd/");

        //文件名的处理：UUID.获取到的文件的扩展名
        String fileName = UUID.randomUUID().toString();
        String ext = ".jpg";
        String key = folder + fileName + ext;

        // 上传网络流。
        InputStream inputStream = null;
        try {
            inputStream = new URL(url).openStream();
        } catch (IOException e) {
            throw new GuliException(ResultCodeEnum.CODE_ERROR);
        }
        ossClient.putObject(bucketName, key, inputStream);

        // 关闭OSSClient。
        ossClient.shutdown();

        // 返回文件的url
        return "https://" + bucketName + "." + endpoint + "/" + key;
    }
}
